package cn.wolfcode.wolf2w.business.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import cn.wolfcode.wolf2w.common.core.domain.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.wolfcode.wolf2w.common.security.annotation.RequiresPermissions;
import cn.wolfcode.wolf2w.business.api.domain.Question;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.core.web.controller.BaseController;
import cn.wolfcode.wolf2w.common.core.utils.poi.ExcelUtil;


/**
 * 提问信息Controller
 *
 * @author libai
 * @date 2024-08-10
 */
@RestController
@RequestMapping("/admin/questions")
@Api(value = "提问操作前端后管控制器接口",description ="questionsAdmin资源管理器" )
public class QuestionAdminController extends BaseController {
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询提问信息列表
     */
    @ApiOperation(value = "admin查询提问信息列表")
    @RequiresPermissions("business:question:list")
    @GetMapping("/list")
    public R<IPage<Question>> list(QuestionQuery qo) {
        return R.ok(questionService.queryPageAdmin(qo));
    }


    /**
     * 导出提问信息列表
     */
    @ApiOperation(value = "admin导出提问信息列表")
    @RequiresPermissions("business:question:export")
//    @Log(title = "提问信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question) {
        List<Question> list = questionService.list();
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "提问信息数据");
    }

    /**
     * 获取提问信息详细信息
     */
    @ApiOperation(value = "admin获取提问信息列表")
    @ApiImplicitParam(value = "问题id", name = "id", dataType = "Long", readOnly = true)
    @RequiresPermissions("business:question:query")
    @GetMapping(value = "/{id}")
    public R<Question> getInfo(@PathVariable("id") Long id) {
        return R.ok(questionService.getById(id));
    }

    /**
     * 新增提问信息
     */
    @ApiOperation(value = "admin新增提问信息")
    @RequiresPermissions("business:question:add")
//    @Log(title = "提问信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody Question question) {
        Long uid = SecurityContextHolder.getUserId();
        return toAjax(questionService.saveQuestion(question,uid));
    }

    /**
     * 修改提问信息
     */
    @ApiOperation(value = "admin修改提问信息")
    @RequiresPermissions("business:question:edit")
//    @Log(title = "提问信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody Question question) {
        return toAjax(questionService.updateById(question));
    }

    /**
     * 删除提问信息
     */
    @ApiOperation(value = "admin删除提问信息")
    @ApiImplicitParam(value = "提问id", name = "ids", dataType = "Long", readOnly = true)
    @RequiresPermissions("business:question:remove")
//    @Log(title = "提问信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids) {
        return toAjax(questionService.removeByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
